package com.shuwen.gcdj.controller.dept;

import com.shuwen.gcdj.bean.dept.request.DepUpdateReq;
import com.shuwen.gcdj.bean.dept.response.DeptDetailResp;
import com.shuwen.gcdj.common.api.CommonPage;
import com.shuwen.gcdj.common.api.CommonResult;
import com.shuwen.gcdj.bean.AddResp;
import com.shuwen.gcdj.bean.dept.request.DeptAddReq;
import com.shuwen.gcdj.bean.dept.request.DeptListReq;
import com.shuwen.gcdj.bean.dept.response.DeptListItemResp;
import com.shuwen.gcdj.common.annotation.JWTAuth;
import com.shuwen.gcdj.common.enums.ErrorCode;
import com.shuwen.gcdj.common.enums.LogModuleEnum;
import com.shuwen.gcdj.common.enums.LogTypeEnum;
import com.shuwen.gcdj.common.sysLog.AddSysLogUtil;
import com.shuwen.gcdj.controller.AuthController;
import com.shuwen.gcdj.dto.account.InitUserDto;
import com.shuwen.gcdj.service.dept.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@Api(tags = "用户中心 - 部门管理")
@RestController
@RequestMapping("/dept")
@Slf4j
public class DeptController extends AuthController {

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private AddSysLogUtil addSysLogUtil;

    @ApiOperation(value = "获取所有部门")
    @GetMapping(value = "/getAllList")
    @ResponseBody
    @JWTAuth
    public CommonResult<List<DeptListItemResp>> getAllList() {
        CommonResult<List<DeptListItemResp>> allList = departmentService.getAllList();
        return allList;
    }

    @ApiOperation(value = "部门搜索-分页")
    @PostMapping(value = "/getPagingList")
    @ResponseBody
    @JWTAuth
    public CommonResult<CommonPage<DeptListItemResp>> getAllList(@Valid @RequestBody DeptListReq req) {
        CommonResult<CommonPage<DeptListItemResp>> pagingList = departmentService.getPagingList(req);
        return pagingList;
    }

    @ApiOperation(value = "添加部门")
    @PostMapping(value = "/add")
    @ResponseBody
    @JWTAuth
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<AddResp> add(HttpServletRequest request, @Valid @RequestBody DeptAddReq req) {
        InitUserDto initUserDto = initUser(request);
        CommonResult<AddResp> addRespCommonResult = departmentService.addDept(req, initUserDto);
        Long code = addRespCommonResult.getCode();
        if(code.equals(ErrorCode.SUCCESS.getCode())){
            addSysLogUtil.addLog(initUserDto, LogTypeEnum.LOG_TYPE_ADD.getTypeNum(), LogModuleEnum.LOG_MODULE_DEPT.getModuleNum(),"添加部门",req);
        }
        return addRespCommonResult;
    }
    @ApiOperation(value = "修改部门")
    @PostMapping(value = "/update")
    @ResponseBody
    @JWTAuth
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<String> update(HttpServletRequest request, @Valid @RequestBody DepUpdateReq req) {
        InitUserDto initUserDto = initUser(request);
        CommonResult<String> Result = departmentService.updateDept(req, initUserDto);
        Long code = Result.getCode();
        if(code.equals(ErrorCode.SUCCESS.getCode())){
            addSysLogUtil.addLog(initUserDto, LogTypeEnum.LOG_TYPE_DELETE.getTypeNum(), LogModuleEnum.LOG_MODULE_DEPT.getModuleNum(),"修改部门",req);
        }
        return Result;
    }
    @ApiOperation(value = "删除部门")
    @PostMapping(value = "/delete/{ids}")
    @ResponseBody
    @JWTAuth
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<String> delete(HttpServletRequest request,  @PathVariable String ids) {
        InitUserDto initUserDto = initUser(request);
        CommonResult<String> delete = departmentService.delete(ids, initUserDto);
        Long code = delete.getCode();
        if(code.equals(ErrorCode.SUCCESS.getCode())){
            addSysLogUtil.addLog(initUserDto, LogTypeEnum.LOG_TYPE_DELETE.getTypeNum(), LogModuleEnum.LOG_MODULE_DEPT.getModuleNum(),"删除部门为",ids);
        }
        return delete;
    }
    @ApiOperation(value = "启用部门")
    @PostMapping(value = "/enable/{id}")
    @ResponseBody
    @JWTAuth
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<String> enable(HttpServletRequest request,  @PathVariable String id) {
        InitUserDto initUserDto = initUser(request);
        CommonResult<String> enable = departmentService.enable(id, initUserDto);
        Long code = enable.getCode();
        if(code.equals(ErrorCode.SUCCESS.getCode())){
            addSysLogUtil.addLog(initUserDto, LogTypeEnum.LOG_TYPE_DELETE.getTypeNum(), LogModuleEnum.LOG_MODULE_DEPT.getModuleNum(),"启用部门为",id);
        }
        return enable;
    }
    @ApiOperation(value = "部门详情")
    @PostMapping(value = "/depDetail/{id}")
    @ResponseBody
    @JWTAuth
    public CommonResult<DeptDetailResp> depDetail(HttpServletRequest request, @PathVariable String id) {
        CommonResult<DeptDetailResp> departmentCommonResult = departmentService.depDetail(id);
        return departmentCommonResult;
    }
}
